
package com.filldream.fastboot.web.controller.pc;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.filldream.fastboot.base.entity.QuartzJob;
import com.filldream.fastboot.base.entity.QuartzLog;
import com.filldream.fastboot.base.request.IdListSTO;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.job.JobLogPageSTO;
import com.filldream.fastboot.base.request.pc.job.JobPageSTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.BasePage;
import com.filldream.fastboot.common.entity.P;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.SpringContextHolder;
import com.filldream.fastboot.core.service.ScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "PC-定时任务")
@RequestMapping(value = "/pc/schedule")
public class PcScheduleJobController {

    private ScheduleJobService jobService;

    @ApiOperation("查询定时任务")
    @GetMapping("/page")
    @RolePermissions("schedule:page")
    public P<QuartzJob> queryQuartzJob(JobPageSTO pageSTO){

        LambdaQueryWrapper<QuartzJob> sql = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank( pageSTO.getKeyContent() )){
            sql.and(wq -> wq.like(QuartzJob::getBeanName,pageSTO.getKeyContent())
                    .or().like(QuartzJob::getJobName,pageSTO.getKeyContent())
                    .or().like(QuartzJob::getMethodName,pageSTO.getKeyContent()));
        }
        sql.orderByDesc(QuartzJob::getCreateTime);

        return P.autoSuccess(jobService.page(pageSTO.page(),sql));

    }

    @Log("新增定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping("/add")
    @RolePermissions("schedule:add")
    public B createQuartzJob(@Validated @RequestBody QuartzJob sto){
        sto.setId(null);
        SpringContextHolder.checkServiceBean(sto.getBeanName());
        jobService.add(sto);
        return B.success();
    }

    @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PostMapping("/modify")
    @RolePermissions("schedule:modify")
    public B updateQuartzJob(@RequestBody QuartzJob sto){
        SpringContextHolder.checkServiceBean(sto.getBeanName());
        jobService.modify(sto);
        return B.success();
    }

    @Log("执行定时任务")
    @ApiOperation("执行定时任务")
    @PostMapping(value = "/exec")
    @RolePermissions("schedule:exec")
    public B executionQuartzJob(@RequestBody IdSTO idSTO){
        jobService.execution(jobService.getById(idSTO.getId()));
        return B.success();
    }

    @Log("更改定时任务状态")
    @ApiOperation("更改定时任务状态")
    @PostMapping(value = "/modifyStatus")
    @RolePermissions("schedule:modifyStatus")
    public B updateQuartzJobStatus(@RequestBody IdSTO idSTO){
        jobService.updateIsPause(jobService.getById(idSTO.getId()));
        return B.success();
    }

    @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @PostMapping("/del")
    @RolePermissions("schedule:del")
    public B deleteQuartzJob(@RequestBody IdListSTO idListSTO){
        jobService.delete(idListSTO.getIds());
        return B.success();
    }

    @Log("任务详情")
    @ApiOperation("任务详情")
    @GetMapping("/get")
    @RolePermissions("schedule:get")
    public R<QuartzJob> get(IdSTO idSTO){
        return R.successd(jobService.getById( idSTO.getId() ));
    }

    @ApiOperation("任务日志列表")
    @GetMapping("/jobLogPage")
    @RolePermissions("schedule:jobLogPage")
    public P<QuartzLog> jobLogPage(JobLogPageSTO sto){
        return P.autoSuccess(jobService.jobLogPage(sto));
    }


    @Log("任务日志详情")
    @ApiOperation("日志详情")
    @GetMapping("/getJobLog")
    @RolePermissions("schedule:getJobLog")
    public R<QuartzLog> getJobLog(IdSTO sto){
        return R.autoSuccess(jobService.getJobLog(sto.getId()));
    }

    @Log("清空任务日志")
    @ApiOperation("清空任务日志")
    @PostMapping("/clearJobLog")
    @RolePermissions("schedule:clearJobLog")
    public B clearJobLog(){
        jobService.clearJobLog();
        return B.success();
    }

}
